import { AppRootProps, createAppProvider } from 'react-app-provider';
import { AppService } from './AppService';

const [AppRoot, useAppContext] = createAppProvider(new AppService());

export const useApp = (): AppService => {
  const { app } = useAppContext();
  if (app == null) {
    throw new Error('App instance not specified');
  }
  return app;
};

export function App({ children, ...props }: AppRootProps<AppService>) {
  // app hooks injections
  return (
    <AppRoot {...props}>{children}</AppRoot>
  );
}
